<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>0</h1>
<h2>0</h2>

<script>
    let count=0;
    //开启定时器
    // setInterval(function () {
    //     count++;
    //     let n1=document.querySelector("h1");
    //      n1.innerText=count;
    // },1000)
    function f() {
        count+=2;
        let h1=document.querySelector("h1");
        h1.innerText=count;
        if(count==10){
            clearInterval(timer)
        }
    }
    //开启定时器,当方法作为参数进行传递时不写()
    let timer= setInterval(f,1000)

    //开启只执行一次的定时器
    setTimeout( function () {
        alert("时间到了")
    },2000)
</script>
</body>
</html>